package com.neulogistics.contrib.procuredcenter.domain.model.enumeration;

import com.neulogistics.contrib.procuredcenter.domain.exception.ProcuredCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class PurchaseType extends Enumeration {

    public static final int Purchase_Id = 1;
    public static final int Return_Id = 2;

    public static PurchaseType Purchase = new PurchaseType(Purchase_Id, "Purchase");
    public static PurchaseType Return = new PurchaseType(Return_Id, "Return");


    protected PurchaseType(int id, String name) {
        super(id, name);
    }

    private static List<PurchaseType> list = Arrays.asList(Purchase, Return);

    public static List<PurchaseType> getList(){return list;}

    public static PurchaseType fromName(String name) throws ProcuredCenterDomainException {
        Optional<PurchaseType> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new ProcuredCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            PurchaseType.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
